import React, {useState} from "react";
import {Button, Form, message, Upload} from "antd";
import {UploadOutlined} from "@ant-design/icons";

function UploadFile(props) {
    const fileList=props["fileList"];
    const setFileList=props["setFileList"];

    const uploadProps = {
        maxCount: 1,
        onRemove: file => {
            const index = fileList.indexOf(file);
            const newFileList = fileList.slice();
            newFileList.splice(index, 1);
            setFileList(newFileList);
        },
        beforeUpload: file => {
            const patter = /image/;
            if (patter.test(file.type)) {
                setFileList([file]);
            } else {
                message.error("请上传图片文件").then(() => {
                });
            }
            return false;
        },
        fileList,
    };

    return(
        <Upload {...uploadProps}>
            <Button icon={<UploadOutlined/>}>选择文件</Button>
        </Upload>
    )
}

export default UploadFile;